package com.yang.controller;

import com.yang.service.LoginService;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    private static Logger logger = Logger.getLogger(LoginController.class);


    @Resource
    LoginService loginService;

    /**
     * 登陆
     * @param request
     * @param num
     * @param password
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> login(HttpServletRequest request,String num,String password){
        Map<String,Object> map = new HashMap<String, Object>();
        if(num == null || password == null || num.equals("") || password.equals("")){
            map.put("info","输入不能为空！");
            return map;
        }
        map = loginService.Login(request,num,password);
        logger.info(num+password);
        return map;
    }

    /**
     * 找回密码发送code
     * @param emil
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = "getCodeForPasswor",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getCodeForPasswor(String emil){
        Map<String,Object> map = new HashMap<String, Object>();
        if(emil == null || emil.equals("")){
            map.put("info","邮箱号不为空！");
            return map;
        }
        map = loginService.getCodeByemil(emil);
        return map;
    }

    /**
     * 重置密码
     * @param emil
     * @param password
     * @param code
     * @return
     */
    @RequestMapping(produces = "application/json;charset=UTF-8",value = "resetPassword",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> resetPassword (String emil,String password,String code){
        Map<String,Object> map = new HashMap<String, Object>();
        if(emil == null || password == null ||emil.equals("") || password.equals("")){
            map.put("info","输入不能为空！");
            logger.error("输入存在空！");
            return map;
        }
        map = loginService.resetPasswordService(emil,password,code);
        return map;
    }

    /**
     * 退出登陆
     * @param request
     * @param response
     */
    @RequestMapping(produces = "application/json;charser=UTF-8",value = "loginOut",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public void loginOut(HttpServletRequest request,HttpServletResponse response){
        logger.info("loginOut");
        request.getSession().removeAttribute("message");
        request.getSession().removeAttribute("identfication");
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.html");
        try {
            requestDispatcher.forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("跳转失败！");
        }

    }

    @RequestMapping(produces = "application/json;charser=UTF-8",value = {"admin/roomInfomation","customer/roomInfomation"},method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> roomCondition(){
        Map<String,Object> map ;
        map = loginService.roomConditionService();
        return map;
    }

}
